<template>
    <div class="three-column-layout">
         <!-- 动态背景 -->
        <TechBackground 
            intensity="medium"
            color-theme="blue"
        />
      <!-- 主内容区 -->
      <el-row :gutter="20" class="main-content">
        <!-- 左列 -->
        <el-col :xs="24" :sm="8" :md="7" :lg="6" :xl="5">
          <div class="column-content">
            <!-- 左上直方图 -->
            <div class="chart-container top-left">
              <div class="chart-title">
                <span class="title-text">销售趋势</span>
                <div class="title-accent"></div>
              </div>
              <div class="chart-content">
                <div class="histogram">
                  <TechChart 
                    type="line"
                    :data="salesTrendData"
                    :loading="salesTrendLoading"
                    color-theme="cyan"
                  />
                </div>
              </div>
            </div>
            
            <!-- 左下直方图 -->
            <div class="chart-container bottom-left">
              <div class="chart-title">
                <span class="title-text">用户增长</span>
                <div class="title-accent"></div>
              </div>
              <div class="chart-content">
                <div class="histogram">
                  <TechChart 
                    type="line"
                    :data="userGrowthData"
                    :loading="userGrowthLoading"
                    color-theme="blue"
                  />
                </div>
              </div>
            </div>
          </div>
        </el-col>
        
        <!-- 中列 -->
        <el-col :xs="24" :sm="16" :md="10" :lg="12" :xl="14">
          <div class="column-content">
            <div class="middle-top-section">
              <!-- 中间雷达图 -->
              <div class="chart-container radar-container">
                <div class="chart-title">
                  <span class="title-text">性能指标雷达图</span>
                  <div class="title-accent"></div>
                </div>
                <div class="chart-content">
                  <TechChart 
                    type="radar"
                    :data="performanceData"
                    :loading="performanceLoading"
                    color-theme="purple"
                  />
                </div>
              </div>
              <div class="metrics-container">
                <div class="chart-title">
                  <span class="title-text">关键指标</span>
                  <div class="title-accent"></div>
                </div>
                <div class="metrics-content">
                  <div class="metric-card" v-for="metric in keyMetrics" :key="metric.name">
                    <div class="metric-icon" :style="{ backgroundColor: metric.color }">
                      <i :class="metric.icon"></i>
                    </div>
                    <div class="metric-info">
                      <div class="metric-value">{{ metric.value }}</div>
                      <div class="metric-name">{{ metric.name }}</div>
                      <div class="metric-trend" :class="metric.trend">
                        <i :class="metric.trendIcon"></i>
                        {{ metric.change }}
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            
            
            <!-- 中间表格 -->
            <div class="table-container middle-bottom">
              <div class="table-title">
                <span class="title-text">用户信息表</span>
                <div class="title-accent"></div>
              </div>
              <div class="table-content">
                <TechTable 
                  :table-data="tableData"
                  :total="totalRecords"
                  :page-size="pageSize"
                  v-model:current-page="currentPage"
                />
              </div>
            </div>
          </div>
        </el-col>
        
        <!-- 右列 -->
        <el-col :xs="24" :sm="8" :md="7" :lg="6" :xl="5">
          <div class="column-content">
            <!-- 右上直方图 -->
            <div class="chart-container top-right">
              <div class="chart-title">
                <span class="title-text">访问来源</span>
                <div class="title-accent"></div>
              </div>
              <div class="chart-content">
                <div class="histogram">
                  <TechChart 
                    type="pie"
                    :data="trafficSourceData"
                    :loading="trafficSourceLoading"
                    color-theme="cyan"
                  />
                </div>
              </div>
            </div>
            
            <!-- 右下直方图 -->
            <div class="chart-container bottom-right">
              <div class="chart-title">
                <span class="title-text">区域销售</span>
                <div class="title-accent"></div>
              </div>
              <div class="chart-content">
                <div class="histogram">
                  <TechChart 
                    :data="regionSalesData"
                    :loading="regionSalesLoading"
                    color-theme="cyan"
                  />
                </div>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
  </template>
  
  <script setup>
  import { getSalesTrend,getUserGrowth,getRegionSales,gettrafficSource,getAllTableData,getPerformance } from '@/api';
  import TechBackground from '@/components/DataVision/TechBackground.vue'
  import TechChart from '@/components/DataVision/TechChart.vue'
  import TechTable from '@/components/DataVision/TechTable.vue'
  import { onMounted,ref } from 'vue'

  // 关键指标数据
const keyMetrics = ref([
  {
    name: '用户活跃度',
    value: '87.5%',
    change: '+5.2%',
    trend: 'up',
    trendIcon: 'el-icon-top',
    color: '#43e97b',
    icon: 'el-icon-user'
  },
  {
    name: '平均响应时间',
    value: '128ms',
    change: '-15ms',
    trend: 'down',
    trendIcon: 'el-icon-bottom',
    color: '#00f2fe',
    icon: 'el-icon-time'
  },
  {
    name: '错误率',
    value: '0.45%',
    change: '-0.12%',
    trend: 'down',
    trendIcon: 'el-icon-bottom',
    color: '#ff0844',
    icon: 'el-icon-warning'
  }
])

  // 数据状态
  const salesTrendData = ref({})
  const userGrowthData = ref({})
  const trafficSourceData = ref({})
  const regionSalesData = ref({})
  const performanceData = ref({})

  // 表格数据
  const tableData = ref([])
  const totalRecords = ref(0)
  const pageSize = ref(3)
  const currentPage = ref(1)
  
  // 加载状态
  const salesTrendLoading = ref(true) 
  const userGrowthLoading = ref(true)
  const trafficSourceLoading =  ref(true);
  const regionSalesLoading = ref(true)
  const performanceLoading = ref(true)
  // 错误状态
  const error = ref(null)

// 数据获取函数
const fetchAllData = async () => {
  try {
    error.value = null
    
    // 并行请求所有数据
    const [
      salesResponse,
      userGrowthResponse,
      trafficSourceResponse,
      regionSalesResponse,
      performanceResponse
    ] = await Promise.all([
      getSalesTrend(),
      getUserGrowth(),
      gettrafficSource(),
      getRegionSales(),
      getPerformance()
    ])
    
    // 更新数据
    salesTrendData.value = salesResponse.data;
    userGrowthData.value = userGrowthResponse.data;
    trafficSourceData.value = trafficSourceResponse.data;
    regionSalesData.value = regionSalesResponse.data;
    performanceData.value = performanceResponse.data;
    console.log(performanceData.value);
    
  } catch (err) {
    console.error('数据获取失败:', err)
    error.value = '数据加载失败，请刷新页面重试'
  } finally {
    // 更新加载状态
    salesTrendLoading.value = false;
    userGrowthLoading.value = false;
    trafficSourceLoading.value = false;
    regionSalesLoading.value = false;
    performanceLoading.value = false;
  }
}

// 获取表格数据
const fetchTableData = async () => {
  try {
    const response = await getAllTableData();
    console.log(response.data);
    // const data = await response.json()
    tableData.value = response.data
    totalRecords.value = response.data.length;
  } catch (error) {
    console.error('Failed to fetch table data:', error)
  }
}

  onMounted(async ()=>{
    fetchAllData();
    fetchTableData();
  })

  </script>
  
  <style lang="scss" scoped>
  // 科技风暗黑系列Sass变量
  $bg-primary: #0a0e17;
  $bg-secondary: #131a2a;
  $bg-card: #1a2235;
  $text-primary: #e2e8f0;
  $text-secondary: #94a3b8;
  $accent-blue: #00f2fe;
  $accent-purple: #4facfe;
  $accent-green: #43e97b;
  $accent-red: #ff0844;
  $border-color: #2d3748;
  $glow-blue: 0 0 10px rgba(0, 242, 254, 0.7);
  $glow-purple: 0 0 10px rgba(79, 172, 254, 0.7);
  $glow-green: 0 0 10px rgba(67, 233, 123, 0.7);
  $glow-red: 0 0 10px rgba(255, 8, 68, 0.7);
  
  .three-column-layout {
    position: relative;
    width: 100%;
    min-height: 100vh;
    background: linear-gradient(135deg, $bg-primary 0%, $bg-secondary 100%);
    padding: 20px;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    
    .main-content {
        position: relative;
        z-index: 1;
        min-height: calc(100vh - 40px);
      
      .column-content {
        display: flex;
        flex-direction: column;
        height: 100%;
        justify-content: center;
        
        @media (max-width: 768px) {
          justify-content: flex-start;
        }
      }
    }
    
    /* 图表容器样式 */
    .chart-container {
      background: $bg-card;
      border-radius: 8px;
      padding: 15px;
      height: 300px;
      display: flex;
      flex-direction: column;
      margin-bottom: 20px;
      transition: all 0.3s ease;
      border: 1px solid $border-color;
      box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
      position: relative;
      overflow: hidden;
      
      &::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        height: 2px;
        background: linear-gradient(90deg, $accent-blue, $accent-purple);
        box-shadow: $glow-blue;
      }
      
      &:last-child {
        margin-bottom: 0;
      }
      
      &:hover {
        transform: translateY(-5px);
        box-shadow: 0 8px 25px rgba(0, 0, 0, 0.5);
      }
      
      .chart-title {
        font-size: 16px;
        font-weight: 600;
        margin-bottom: 15px;
        color: $text-primary;
        padding-bottom: 10px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        
        .title-text {
          position: relative;
          z-index: 1;
        }
        
        .title-accent {
          width: 30px;
          height: 3px;
          background: linear-gradient(90deg, $accent-blue, $accent-purple);
          border-radius: 2px;
          box-shadow: $glow-blue;
        }
      }
      
      .chart-content {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
    
    /* 直方图样式 */
    .histogram {
      width: 100%;
      height: 100%;
      border-radius: 4px;
      display: flex;
      align-items: flex-end;
      justify-content: space-around;
      padding: 10px;
      background: rgba(255, 255, 255, 0.02);
      
      .bar {
        width: 22%;
        border-radius: 4px 4px 0 0;
        transition: all 0.3s ease;
        position: relative;
        
        &::after {
          content: '';
          position: absolute;
          bottom: 0;
          left: 0;
          right: 0;
          height: 100%;
          background: linear-gradient(to top, rgba(255,255,255,0.1), transparent);
          border-radius: 4px 4px 0 0;
        }
        
        &:hover {
          opacity: 0.9;
          transform: translateY(-5px);
          filter: brightness(1.2);
        }
      }
    }
    
    /* 雷达图样式 */
    .radar-chart {
      width: 100%;
      height: 100%;
      border-radius: 4px;
      display: flex;
      align-items: center;
      justify-content: center;
      background: linear-gradient(135deg, #0f1b2d 0%, #1a2c42 100%);
      color: $text-primary;
      font-size: 18px;
      font-weight: bold;
      position: relative;
      overflow: hidden;
      border: 1px solid rgba(79, 172, 254, 0.3);
      
      .radar-grid {
        position: relative;
        width: 200px;
        height: 200px;
        border-radius: 50%;
        border: 1px solid rgba(79, 172, 254, 0.5);
        
        .grid-circle {
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          border-radius: 50%;
          border: 1px solid rgba(79, 172, 254, 0.3);
          
          &:nth-child(1) {
            width: 180px;
            height: 180px;
          }
          
          &:nth-child(2) {
            width: 120px;
            height: 120px;
          }
          
          &:nth-child(3) {
            width: 60px;
            height: 60px;
          }
        }
        
        .radar-scan {
          position: absolute;
          top: 50%;
          left: 50%;
          width: 200px;
          height: 200px;
          border-radius: 50%;
          transform: translate(-50%, -50%);
          background: conic-gradient(transparent, rgba(0, 242, 254, 0.3), transparent);
          animation: radarScan 3s linear infinite;
        }
      }
      
      .radar-label {
        position: absolute;
        bottom: 20px;
        left: 0;
        right: 0;
        text-align: center;
        font-size: 14px;
        color: $accent-blue;
        text-shadow: $glow-blue;
      }
    }
    
    /* 表格容器样式 */
    .table-container {
      background: $bg-card;
      border-radius: 8px;
      padding: 15px;
      height: 300px;
      display: flex;
      flex-direction: column;
      transition: all 0.3s ease;
      border: 1px solid $border-color;
      box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
      position: relative;
      overflow: hidden;
      
      &::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        height: 2px;
        background: linear-gradient(90deg, $accent-green, $accent-blue);
        box-shadow: $glow-green;
      }
      
      &:hover {
        transform: translateY(-5px);
        box-shadow: 0 8px 25px rgba(0, 0, 0, 0.5);
      }
      
      .table-title {
        font-size: 16px;
        font-weight: 600;
        margin-bottom: 15px;
        color: $text-primary;
        padding-bottom: 10px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        
        .title-text {
          position: relative;
          z-index: 1;
        }
        
        .title-accent {
          width: 30px;
          height: 3px;
          background: linear-gradient(90deg, $accent-green, $accent-blue);
          border-radius: 2px;
          box-shadow: $glow-green;
        }
      }
      
      .table-content {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        background: rgba(10, 14, 23, 0.5);
        border-radius: 4px;
        color: $text-secondary;
        font-size: 16px;
        position: relative;
        overflow: hidden;
        border: 1px solid rgba(255, 255, 255, 0.05);
        
        .data-grid {
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          
          .grid-line {
            position: absolute;
            background: rgba(79, 172, 254, 0.1);
            
            &:nth-child(1) {
              top: 33.33%;
              left: 0;
              right: 0;
              height: 1px;
            }
            
            &:nth-child(2) {
              top: 66.66%;
              left: 0;
              right: 0;
              height: 1px;
            }
            
            &:nth-child(3) {
              left: 50%;
              top: 0;
              bottom: 0;
              width: 1px;
            }
          }
          
          .data-point {
            position: absolute;
            width: 6px;
            height: 6px;
            border-radius: 50%;
            background: $accent-green;
            box-shadow: $glow-green;
            transform: translate(-50%, -50%);
            animation: pulse 2s infinite;
            
            &:nth-child(2n) {
              background: $accent-blue;
              box-shadow: $glow-blue;
              animation-delay: 0.5s;
            }
            
            &:nth-child(3n) {
              background: $accent-purple;
              box-shadow: $glow-purple;
              animation-delay: 1s;
            }
          }
        }
        
        .table-label {
          position: relative;
          z-index: 1;
          color: $accent-green;
          text-shadow: $glow-green;
        }
      }
    }
    
    /* 动画定义 */
    @keyframes radarScan {
      0% {
        transform: translate(-50%, -50%) rotate(0deg);
      }
      100% {
        transform: translate(-50%, -50%) rotate(360deg);
      }
    }
    
    @keyframes pulse {
      0%, 100% {
        opacity: 0.7;
        transform: translate(-50%, -50%) scale(1);
      }
      50% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1.2);
      }
    }
    
    /* 响应式调整 */
    @media (max-width: 1280px) {
      .chart-container,
      .table-container {
        height: 280px;
      }
    }
    
    @media (max-width: 992px) {
      .chart-container,
      .table-container {
        height: 250px;
      }
    }
    
    @media (max-width: 768px) {
      padding: 10px;
      
      .chart-container,
      .table-container {
        height: 220px;
        margin-bottom: 15px;
      }
      
      .column-content {
        .chart-container:first-child {
          margin-bottom: 15px;
        }
      }
    }
    
    @media (max-width: 480px) {
      .chart-container,
      .table-container {
        height: 200px;
      }
      
      .radar-chart .radar-grid {
        width: 150px;
        height: 150px;
        
        .grid-circle:nth-child(1) {
          width: 135px;
          height: 135px;
        }
        
        .grid-circle:nth-child(2) {
          width: 90px;
          height: 90px;
        }
        
        .grid-circle:nth-child(3) {
          width: 45px;
          height: 45px;
        }
        
        .radar-scan {
          width: 150px;
          height: 150px;
        }
      }
    }
  }

  .middle-top-section {
    display: flex;
    gap: 20px;
    height: 300px; // 固定总高度
    margin-bottom: 20px;
    
    @media (max-width: 1200px) {
      flex-direction: column;
      height: auto;
    }
  }

  .radar-container {
    flex: 2; // 雷达图占2/3宽度
    height: 100%; // 继承父容器高度
  }

  .metrics-container {
    flex: 1; // 指标卡片占1/3宽度
    height: 100%; // 继承父容器高度
    background: rgba(26, 34, 53, 0.85);
    border-radius: 8px;
    padding: 15px;
    border: 1px solid rgba(45, 55, 72, 0.6);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(5px);
    display: flex;
    flex-direction: column;
    
    .metrics-content {
      flex: 1; // 占据剩余空间
      display: flex;
      flex-direction: column;
      gap: 8px; // 减小间距
      overflow: hidden; // 防止溢出
    }
    .title-text {
      color: $text-primary;
    }
  }

  .metric-card {
    display: flex;
    align-items: center;
    padding: 8px; // 减小内边距
    background: rgba(10, 14, 23, 0.6);
    border-radius: 6px;
    border: 1px solid rgba(79, 172, 254, 0.2);
    transition: all 0.3s ease;
    flex: 1; // 每个卡片平均分配高度
    min-height: 0; // 允许收缩
    
    &:hover {
      background: rgba(0, 242, 254, 0.1);
      border-color: rgba(0, 242, 254, 0.4);
      transform: translateY(-1px);
    }
    
    .metric-icon {
      width: 32px; // 减小图标尺寸
      height: 32px;
      border-radius: 6px;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 10px;
      flex-shrink: 0; // 防止图标被压缩
      
      i {
        color: white;
        font-size: 16px; // 减小图标大小
      }
    }
    
    .metric-info {
      flex: 1;
      min-width: 0; // 允许文本截断
      
      .metric-value {
        font-size: 16px; // 减小字体大小
        font-weight: bold;
        color: #e2e8f0;
        margin-bottom: 2px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      
      .metric-name {
        font-size: 11px; // 减小字体大小
        color: #94a3b8;
        margin-bottom: 2px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      
      .metric-trend {
        font-size: 10px; // 减小字体大小
        display: flex;
        align-items: center;
        
        &.up {
          color: #43e97b;
        }
        
        &.down {
          color: #ff0844;
        }
        
        i {
          margin-right: 3px;
          font-size: 9px;
        }
      }
    }
  }

  // 响应式调整
  @media (max-width: 1200px) {
    .middle-top-section {
      .radar-container,
      .metrics-container {
        flex: none;
        height: 300px;
      }
    }
  }

  @media (max-width: 768px) {
    .middle-top-section {
      .radar-container,
      .metrics-container {
        height: 250px;
      }
    }
    
    .metric-card {
      padding: 6px;
      
      .metric-icon {
        width: 28px;
        height: 28px;
        
        i {
          font-size: 14px;
        }
      }
      
      .metric-value {
        font-size: 14px;
      }
    }
  }
  </style>